
package com.mephi.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import org.apache.log4j.Logger;
import com.mephi.MephistoChess;
import com.mephi.controller.GameIteratorWrapper;
import com.mephi.controller.action.BtnFirstAction;
import com.mephi.controller.action.BtnLastAction;
import com.mephi.controller.action.BtnNextAction;
import com.mephi.controller.action.BtnPreviousAction;
import com.mephi.controller.action.ExportGameAction;
import com.mephi.controller.action.ImportGameAction;
import com.mephi.controller.action.LoadGameAction;
import com.mephi.controller.action.SaveGameAction;
import com.mephi.interfaces.Game;
import com.mephi.interfaces.LoginManager;
import com.mephi.interfaces.Profile;
import com.mephi.model.GameMovesModel;
import com.mephi.model.Move;
import com.mephi.model.profile.LoginManagerImpl;
import com.mephi.model.profile.ProfileImpl;

/**
 * 
 * @author mephisto
 * @since 2011-04-11
 */
public enum GUI
{

	 /**
     * singleton instance of class GUI
     */
    INSTANCE;
    
    private GUI()
    {
    	createMainFrame();
    	//FAKE
    	Profile currentProfile = new ProfileImpl("mephisto");
    	LoginManager loginManager = LoginManagerImpl.INSTANCE;
    	loginManager.setCurrentProfile(currentProfile);
    	//END FAKE
    }
    
	private void createMainFrame()
	{
		EventQueue.invokeLater(new Runnable()
	    {
	        public void run()
	        {
	            fraMainFrame = new MainFrame();
	       		fraMainFrame.setLayout(new BorderLayout());
	    		fraMainFrame.setTitle(APP_NAME);	
	    		createMenu();
	    		/*createToolbar();	 ;*/
	    		//fraMainFrame.add(getPanChessboard(), BorderLayout.CENTER);
	    		//fraMainFrame.add(getPanRightPanel(), BorderLayout.EAST);
	    		//fraMainFrame.add(getPanBottomPanel(), new GBC(1,0,0,0).setFill(GBC.HORIZONTAL).setInsets(20));
	    		fraMainFrame.add(getPanBottomPanel(), BorderLayout.SOUTH);
	    		
	    		fraMainFrame.setVisible(true);
	    		
	        }
	    });
	}
	
	public void repaintMovesPanel()
	{
		if (panRightPanel == null)
			fraMainFrame.add(
					getPanRightPanel(), 
					BorderLayout.EAST);

		//tu jest wczytanie gry z profilu
		//FAKE
    	LoginManager loginManager = LoginManagerImpl.INSTANCE;
    	Profile currentProfile = loginManager.getCurrentProfile();
    	//END FAKE
		GUI.INSTANCE.getPanChessScoreSheet().update(
				new GameMovesModel(currentProfile.getGameMap().get("1")));
		GameIteratorWrapper.INSTANCE.setGameIterator(
				GUI.INSTANCE.getPanChessScoreSheet().getGameModel().getGameModel().gameIterator()); 
	}
	
	public void setStartingPosition()
	{
		fraMainFrame.getPanBoard().clearChessboard();
		fraMainFrame.getPanBoard().setStartingPosition();
	}
	
	public MainFrame getFraMainFrame()
	{
		return fraMainFrame;
	}
	
	public ChessScoreSheet getPanChessScoreSheet()
	{
		return panChessScoreSheet;
	}

	public void setPanChessScoreSheet(ChessScoreSheet panChessScoreSheet)
	{
		this.panChessScoreSheet = panChessScoreSheet;
	}
	
	private JPanel getPanRightPanel()
	{
		panRightPanel = new JPanel();
		panRightPanel.setBackground(Color.DARK_GRAY);
		//FAKE
    	LoginManager loginManager = LoginManagerImpl.INSTANCE;
    	Profile currentProfile = loginManager.getCurrentProfile();
    	//END FAKE
		
		//Profile profile = ProfileMapSingleton.INSTANCE.getProfiles().get("mephisto");
    	panChessScoreSheet = new ChessScoreSheet(
    			new GameMovesModel(currentProfile.getGameMap().get("1")));
		panRightPanel.add(panChessScoreSheet);
		return panRightPanel;
	}
	
	private JPanel getPanBottomPanel()
	{
		panBottomPanel = new JPanel();
		panBottomPanel.setBackground(new Color(200,200,200));
		panBottomPanel.add(getBtnFirst(), BorderLayout.SOUTH);
		panBottomPanel.add(getBtnPrevious(), BorderLayout.SOUTH);
		panBottomPanel.add(getBtnNext(), BorderLayout.SOUTH);
		panBottomPanel.add(getBtnLast(), BorderLayout.SOUTH);
		return panBottomPanel;
	}
	
	private void createMenu()
	{
		mbrMenu = new JMenuBar();
		//okresla pasek menu danej ramki
		fraMainFrame.setJMenuBar(mbrMenu);
		//mbrMenu.setBackground(new Color(233,150,122));
		mbrMenu.add(getMnuFile());
		/*mbrMenu.add(getMnuEdit());
		mbrMenu.add(getMnuLanguage());
		mbrMenu.add(getMnuHelp());
		createContextMenu();*/
	}
	 
	private JMenu getMnuFile()
	{
		mnuFile = new JMenu("File");
		mnuFile.setMnemonic(KeyEvent.VK_F);
		
		mnuFile.add(getMnuFileItemNewLogin());
		mnuFile.add(getMnuFileItemLogIn());
		mnuFile.add(getMnuFileItemLogOut());
		mnuFile.addSeparator();
		mnuFile.add(getMnuFileMnuGame());
		mnuFile.addSeparator();
		mnuFile.add(getMnuFileItemExit());
		
		
		/*mbrMenuMenuFileItemNewLogin=new JMenuItem(
				"Nowy login",KeyEvent.VK_N);
		mbrMenuMenuFileItemNewLogin.setAccelerator(KeyStroke.getKeyStroke(
		          KeyEvent.VK_N, InputEvent.CTRL_MASK));
		mbrMenuMenuFileItemNewLogin.addActionListener(
				new MenuFileItemNewLoginAction());
		mbrMenuMenuFile.add(mbrMenuMenuFileItemNewLogin);
		
		mbrMenuMenuFile.addSeparator();
		
		mbrMenuMenuFileItemLogIn=new JMenuItem("Zaloguj",KeyEvent.VK_L);
		mbrMenuMenuFileItemLogIn.addActionListener(
				new MenuFileItemLogInAction());
		mbrMenuMenuFile.add(mbrMenuMenuFileItemLogIn);
		
		mbrMenuMenuFileItemLogOut=new JMenuItem("Wyloguj",KeyEvent.VK_W);
		mbrMenuMenuFileItemLogOut.addActionListener(new 
				 ActionListener()
				 	{
					 	public void actionPerformed(ActionEvent e)
					 	{
					 		JOptionPane.showMessageDialog(
									null, "Wylogowano", 
									"Informacja", JOptionPane.INFORMATION_MESSAGE);
					 		StudentsGrade.gui.setEnableMenuFileItemRemoveLogin(false);
					 	}				 	
				 	}
				);
		mbrMenuMenuFile.add(mbrMenuMenuFileItemLogOut);
		
		mbrMenuMenuFileItemRemoveLogin=new JMenuItem(
				"Usu\u0144 sw\u00F3j profil",KeyEvent.VK_U);
		mbrMenuMenuFileItemRemoveLogin.addActionListener(
				new MenuFileItemRemoveLoginAction());
		mbrMenuMenuFileItemRemoveLogin.setEnabled(false);
		mbrMenuMenuFile.add(mbrMenuMenuFileItemRemoveLogin);

		mbrMenuMenuFileItemSave=new JMenuItem(
				"Zapisz",KeyEvent.VK_S);
		mbrMenuMenuFileItemSave.setAccelerator(KeyStroke.getKeyStroke(
		          KeyEvent.VK_S, InputEvent.CTRL_MASK));
		mbrMenuMenuFileItemSave.addActionListener(
				new MenuFileItemSaveAction());
		mbrMenuMenuFile.add(mbrMenuMenuFileItemSave);
		
		mbrMenuMenuFile.addSeparator();*/
		
		return mnuFile;
	}
	
	private JMenuItem getMnuFileItemNewLogin()
	{
		mnuFileNewLogin = new JMenuItem("New login",KeyEvent.VK_N);
		mnuFileNewLogin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		//mnuFileNewLogin.addActionListener(new MnuFileItemNewLoginAction());
		return mnuFileNewLogin;
	}
	
	private JMenuItem getMnuFileItemLogIn()
	{
		mnuFileLogIn = new JMenuItem("Log in", KeyEvent.VK_L);
		mnuFileLogIn.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK));
		//mnuFileLogIn.addActionListener(new MnuFileLogInAction());
		return mnuFileLogIn;
	}
	
	private JMenuItem getMnuFileItemLogOut()
	{
		mnuFileLogOut = new JMenuItem("Log out",KeyEvent.VK_O);
		mnuFileLogOut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		return mnuFileLogOut;	
	}
	
	private JMenu getMnuFileMnuGame()
	{
		mnuFileMnuGame = new JMenu("Game");
		mnuFileMnuGame.setMnemonic(KeyEvent.VK_T);
		mnuFileMnuGame.add(getMnuFileMnuGameItemImport());
		mnuFileMnuGame.add(getMnuFileMnuGameItemExport());
		mnuFileMnuGame.addSeparator();
		mnuFileMnuGame.add(getMnuFileMnuGameItemLoad());
		mnuFileMnuGame.add(getMnuFileMnuGameItemSave());
		return mnuFileMnuGame;
	}
	
	private JMenuItem getMnuFileMnuGameItemImport()
	{
		mnuFileMnuGameItemImport = new JMenuItem("Import",KeyEvent.VK_I);
		mnuFileMnuGameItemImport.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent event)
					{
						final String title = "Loading error";
						LoginManager loginManager = LoginManagerImpl.INSTANCE;
						if (loginManager.isSomebodyLoggedIn())
						{
							Profile profile = loginManager.getCurrentProfile();
							ImportGameAction action = new ImportGameAction(profile);
							action.run();	
						}
						else
						{
							String errMsg = "Nobody is currently logged in! Please log in first!";
							logger.warn(errMsg);
							JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(),
									errMsg, title, JOptionPane.ERROR_MESSAGE);
						}
					}
				});
		
		return mnuFileMnuGameItemImport;
	}
	
	private JMenuItem getMnuFileMnuGameItemLoad()
	{
		mnuFileMnuGameItemLoad = new JMenuItem("Load",KeyEvent.VK_L);
		mnuFileMnuGameItemLoad.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent event)
			{
				final String title = "Loading error";
				LoginManager loginManager = LoginManagerImpl.INSTANCE;
				if (loginManager.isSomebodyLoggedIn())
				{
					Profile profile = loginManager.getCurrentProfile();
					LoadGameAction action = new LoadGameAction(profile);
					action.run();	
				}
				else
				{
					String errMsg = "Nobody is currently logged in! Please log in first!";
					logger.warn(errMsg);
					JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(),
							errMsg, title, JOptionPane.ERROR_MESSAGE);
				}					
			}
		});
		
		return mnuFileMnuGameItemLoad;
	}
	
	private JMenuItem getMnuFileMnuGameItemSave()
	{
		mnuFileMnuGameItemSave = new JMenuItem("Save",KeyEvent.VK_S);
		mnuFileMnuGameItemSave.addActionListener(new ActionListener()
		{
			final String title = "Saving error";
			@Override
			public void actionPerformed(ActionEvent event)
			{
				LoginManager loginManager = LoginManagerImpl.INSTANCE;
				if (loginManager.isSomebodyLoggedIn())
				{
					Profile profile = loginManager.getCurrentProfile();
					SaveGameAction action = new SaveGameAction(profile);
					action.run();	
				}
				else
				{
					String errMsg = "Nobody is currently logged in! Please log in first!";
					logger.warn(errMsg);
					JOptionPane.showMessageDialog(MephistoChess.gui.getFraMainFrame(),
							errMsg, title, JOptionPane.ERROR_MESSAGE);
				}	
			}
		});	
		return mnuFileMnuGameItemSave;
	}
	
	private JMenuItem getMnuFileMnuGameItemExport()
	{
		mnuFileMnuGameItemExport = new JMenuItem("Export",KeyEvent.VK_E);
		mnuFileMnuGameItemExport.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent event)
					{
						ExportGameAction action = new ExportGameAction();
						action.run();	
					}
				});
	
		
		return mnuFileMnuGameItemExport;
	}
	 
	private JMenuItem getMnuFileItemExit()
	{
		mnuFileItemExit = new JMenuItem("Close", KeyEvent.VK_F4);
		mnuFileItemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
		mnuFileItemExit.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
				{
					logger.info("Application has been closed!");
					System.exit(0);
				}
			});
		return mnuFileItemExit;
	}

	private JButton getBtnFirst()
	{
		btnFirst = new JButton("<<");
		btnFirst.addActionListener(new BtnFirstAction());
		return btnFirst;
	}
	
	private JButton getBtnPrevious()
	{
		btnPrevious = new JButton("<");
		btnPrevious.addActionListener(new BtnPreviousAction());
		return btnPrevious;
	}

	private JButton getBtnNext()
	{
		btnNext = new JButton(">");
		btnNext.addActionListener(new BtnNextAction());
		return btnNext;
	}
	
	private JButton getBtnLast()
	{
		btnLast = new JButton(">>");
		btnLast.addActionListener(new BtnLastAction());
		return btnLast;
	}

	
	public static String APP_NAME = "Mephisto chess viewer";
	private MainFrame fraMainFrame = null;
	//private MainFrame panChessboard = null;
	private JPanel panRightPanel = null;
	private JPanel panBottomPanel = null;
	private ChessScoreSheet panChessScoreSheet = null;


	private JButton btnNext;
	private JButton btnPrevious;
	private JButton btnLast;
	private JButton btnFirst;
	
	
	
	private JMenuBar mbrMenu = null;
	
	private JMenu mnuFile = null;
	/*private JMenu mbrMenuMenuEdit = null;
	private JMenu mbrMenuMenuData = null;
	private JMenu mbrMenuMenuLanguage = null;
	private JMenu mbrMenuMenuHelp = null;*/
	
	private JMenuItem mnuFileLogIn = null;
	private JMenuItem mnuFileLogOut = null;
	private JMenuItem mnuFileItemExit = null;
	private JMenu mnuFileMnuGame = null;
	private JMenuItem mnuFileMnuGameItemImport = null;
	private JMenuItem mnuFileMnuGameItemSave = null;
	
	private JMenuItem mnuFileMnuGameItemLoad = null;
	private JMenuItem mnuFileMnuGameItemExport = null;
	
	private JMenuItem mnuFileNewLogin = null;
	
	private Logger logger = Logger.getLogger("LOG");	
}
